<?php

class admin_controller_user_manager extends admin_controller
{

    // 登陆后首页
    public function dashboard()
    {
        $template = $this->get_admin_template();
        $template->display();
    }

    // 管理用户列表
    public function listing()
    {
        $order_by = request('order_by', 'id');
        $order_by_dir = request('order_by_dir', 'ASC');
        
        $key = request('key', '');
        $status = request('status', -1);
        
        $model_user_manager = $this->get_admin_model();
        
        $template = $this->get_admin_template();
        $template->set_title('用户列表');
        
        $limit = 12;
        $pagination = bone::get_ui('pagination');
        $pagination->set_limit($limit);
        $pagination->set_total($model_user_manager->get_user_sum($key, $status));
        $pagination->set_page(get('page', 1, 'int'));
        $pagination->update();
        
        $template->set('pagination', $pagination);
        $template->set('order_by', $order_by);
        $template->set('order_by_dir', $order_by_dir);
        $template->set('key', $key);
        $template->set('status', $status);
        
        $template->set('users', $model_user_manager->get_users($key, $status, $order_by, $order_by_dir, $pagination->get_offset(), $limit));
        
        $template->display();
    }

    // 修改用户
    public function edit()
    {
        $id = get('id', 0, 'int');
        
        $user = $this->get_table('user');
        if ($id != 0) $user->load($id);
        
        $template = $this->get_admin_template();
        
        if ($id != 0)
            $template->set_title('修改用户资料');
        else
            $template->set_title('添加新用户');
        
        $template->set('user', $user);
        
        $return = get('return', null);
        if ($return === null) $return = base64_encode($_SERVER['HTTP_REFERER']);
        $template->set('return', $return);
        
        $template->display();
    }

    // 保存修改用户
    public function edit_save()
    {
        $id = post('id', 0, 'int');
        $return = post('return');

        $model = $this->get_admin_model();
        if ($model->edit(post()))
        {
            system_log( ($id == 0)?('添加新用户: '.post('username')):('修改用户  '.post('username').' 资料: '));
            $this->redirect(base64_decode($return), $id == 0 ? '添加用户成功' : '修改用户成功');
        }
        else
            $this->redirect('./?action=user.manager.edit&id[]=' . $id . '&return=' . $return, $model->get_error(), 'error');
    }

    // 设置用户为管理员
    public function ajax_set_admin()
    {
        $id = get('id', 0, 'int');
        $status = get('status', 0, 'int');
        if ($id == 0)
        {
            $this->set_ajax('status', 1);
            $this->set_ajax('description', '参数{id}缺失');
            $this->ajax();
        }
        
        $model = $this->get_admin_model();
        
        if (!$model->set_admin($id, $status))
        {
            $this->set_ajax('status', 2);
            $this->set_ajax('description', $model->get_error());
            $this->ajax();
        }
        
        $this->set_ajax('status', 0);
        $this->set_ajax('description', '设置成功');
        
        $username = bone::get_user($id)->username;
        if ($status == 0)
            system_log('设置 ' . $username . ' 为系统管理员');
        else
            system_log('撤消 ' . $username . ' 的系统管理员权限');
        $this->ajax();
    }

    public function check_username()
    {
        $username = get('username', '');
        
        $model = $this->get_admin_model();
        echo $model->check_username($username) ? 'true' : 'false';
    }

    public function check_email()
    {
        $email = get('email', '');
        
        $model = $this->get_admin_model();
        echo $model->check_email($email) ? 'true' : 'false';
    }

    public function unblock()
    {
        $id = get('id');
        
        $model = $this->get_admin_model();
        if ($model->unblock($id))
        {
            $this->set_message('公开用户账号成功');
            system_log('公开 #'.$id.' 用户 账号 ');
        }
        else
            $this->set_message($model->get_error(), 'error');
        
        $this->redirect($_SERVER['HTTP_REFERER']);
    }

    public function block()
    {
        $id = get('id');
        
        $model = $this->get_admin_model();
        if ($model->block($id))
        {
            $this->set_message('屏蔽用户账号成功');
            system_log('屏蔽 #'.$id.' 用户 账号 ');
        }
        else
            $this->set_message($model->get_error(), 'error');
        
        $this->redirect($_SERVER['HTTP_REFERER']);
    }

    public function delete()
    {
        $id = get('id');
        
        $model = $this->get_admin_model();
        if ($model->delete($id))
        {
            $this->set_message('删除用户成功');
            system_log('删除 #'.$id.' 用户 账号 ');
        }
        else
            $this->set_message($model->get_error(), 'error');
        
        $this->redirect($_SERVER['HTTP_REFERER']);
    }
    
    public function ajax_init_avatar()
    {
        $user_id = get('user_id');
        
        $model = $this->get_admin_model();
        if ($model->init_avatar($user_id))
        {
            system_log('删除 #'.$user_id.' 用户头像 ');
            $this->set_ajax('status', 0);
            $this->set_ajax('description', '删除头像成功');
        }
        else
        {
            $this->set_ajax('status', 1);
            $this->set_ajax('description', $model->get_error());
        }
        
        $this->ajax();

    }

}

?>